package com.zhaoyan.controller;



import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.BoundZSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zhaoyan.entity.Goods;

/**
 * @author zhaoyan
 *
 *	2020年3月2日
 */
@Controller
public class GoodsController {

	
	@Resource
	private RedisTemplate redisTemplate;
	
	/**
	 * list分页
	 */
	@RequestMapping("findByPage")
	public String findByPage(Model model,@RequestParam(defaultValue = "1")Integer pageNum) {
		
		BoundListOperations boundListOps = redisTemplate.boundListOps("goods_list");
		
		
		List<Goods> range = pageUtils(pageNum ,boundListOps); 
												
		
		model.addAttribute("range", range);
		
		model.addAttribute("pageNum", pageNum);
		
		return "list";
	}
	
	/**
	 * 	去排序
	 */
	@RequestMapping("toShowList")
	public String findBySet(Model model,@RequestParam(defaultValue = "1")Integer pageNum) {
		
		BoundListOperations boundListOps = redisTemplate.boundListOps("goods_list");
		
		List<Goods> range = boundListOps.range(0, -1);
		
		
		
		BoundZSetOperations opsSet = redisTemplate.boundZSetOps("goods_zset");
		
		for (Goods goods : range) {
			
			opsSet.add(goods, goods.getPrice().doubleValue());
			
					
		}
		
		
		return "redirect:setList";
	}
	
	
	/**
	 * 	排序页面
	 */
	@RequestMapping("setList")
	public String setList(Model model, @RequestParam(defaultValue = "1")Integer pageNum) {
		
		
		BoundZSetOperations opsSet = redisTemplate.boundZSetOps("goods_zset");
		
		Set<Goods> range = pageUtils(pageNum,opsSet);
		
		model.addAttribute("range", range);
		
		model.addAttribute("pageNum", pageNum);
		
		return "setList";
	}

	
	/**
	 * 	处理分页的方法
	 */
	private Set<Goods> pageUtils(Integer pageNum, BoundZSetOperations opsSet) {
		// TODO Auto-generated method stub
		Set list = null;
		switch (pageNum) {
		case 1:
			list= opsSet.reverseRange(0, 9);
			break;
		case 2:
			list=opsSet.reverseRange(10, 19);
			break;
		case 3:
			list=opsSet.reverseRange(20, 29);
			break;
		case 4:
			list=opsSet.reverseRange(30, 39);
			break;
		case 5:
			list=opsSet.reverseRange(40, 49);
			break;
		case 6:
			list=opsSet.reverseRange(50, 59);
			break;
		case 7:
			list=opsSet.reverseRange(60, 69);
			break;
		case 8:
			list=opsSet.reverseRange(70, 79);
			break;
		case 9:
			list=opsSet.reverseRange(80, 89);
			break;
		case 10:
			list=opsSet.reverseRange(90, 99);
			break;
		case 11:
			list=opsSet.reverseRange(100, 105);
			break;

		default:
			break;
		}
		
		return list;
		
	}


	private List<Goods> pageUtils(Integer pageNum, BoundListOperations boundListOps) {
		
		
		List<Goods> list = null;
		switch (pageNum) {
		case 1:
			list= boundListOps.range(0, 9);
			break;
		case 2:
			list=boundListOps.range(10, 19);
			break;
		case 3:
			list=boundListOps.range(20, 29);
			break;
		case 4:
			list=boundListOps.range(30, 39);
			break;
		case 5:
			list=boundListOps.range(40, 49);
			break;
		case 6:
			list=boundListOps.range(50, 59);
			break;
		case 7:
			list=boundListOps.range(60, 69);
			break;
		case 8:
			list=boundListOps.range(70, 79);
			break;
		case 9:
			list=boundListOps.range(80, 89);
			break;
		case 10:
			list=boundListOps.range(90, 99);
			break;
		case 11:
			list=boundListOps.range(100, 105);
			break;

		default:
			break;
		}
		
		return list;
	}
	
}
